package yelmor.comm100.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import yelmor.comm100.domain.CoUser;
import yelmor.comm100.repository.CoUserRepository;
import yelmor.comm100.util.SecurityUtil;

/**
 * Created by yel on 4/16/16.
 */
@Controller
public class GeneralController {

  @Autowired
  CoUserRepository coUserRepository;

  @RequestMapping("/")
  public String home(Model model) {
    String userName;
    userName = SecurityUtil.getCurrentUserName();
    System.out.println(userName);
    if(userName.equals("anonymousUser")) {
      model.addAttribute("notLogin", true);
    } else {
      model.addAttribute("userName", userName);
      CoUser coUser = coUserRepository.findOne(userName);
      model.addAttribute("coUser", coUser);
      return "redirect: /user/basic";
    }
    return "index-new";
  }

  @RequestMapping("/login")
  public String login() {
    return "login";
  }
}
